#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Action注册管理器
统一管理所有QAction的回调函数
"""

from PyQt5.QtCore import QObject


class ActionRegistry:
    """Action回调函数注册表"""
    
    def __init__(self):
        self._registry = {}
    
    def register(self, action_id, callback):
        """
        注册action回调函数
        
        Args:
            action_id: action标识符
            callback: 回调函数
        """
        self._registry[action_id] = callback
    
    def unregister(self, action_id):
        """注销action"""
        if action_id in self._registry:
            del self._registry[action_id]
    
    def get(self, action_id):
        """获取action回调"""
        return self._registry.get(action_id)
    
    def get_all(self):
        """获取所有注册的回调"""
        return self._registry.copy()
    
    def clear(self):
        """清空注册表"""
        self._registry.clear()


class ActionHandler(QObject):
    """
    Action处理器基类
    
    继承 QObject 以便使用 sender() 方法获取发送信号的 QAction
    """
    
    def __init__(self, registry):
        """
        初始化处理器
        
        Args:
            registry: ActionRegistry实例
        """
        super().__init__()  # 初始化 QObject
        self.registry = registry
        self.register_actions()
    
    def register_actions(self):
        """注册所有action,子类需重写此方法"""
        raise NotImplementedError
    
    # 文件菜单actions
    def on_new_project(self):
        """新建项目"""
        print("新建项目")
    
    def on_open_project(self):
        """打开项目"""
        print("打开项目")
    
    def on_save(self):
        """保存"""
        action = self.sender()  # 获取发送信号的 QAction
        if action:
            print(f"保存 - Action: {action.text()}, Shortcut: {action.shortcut().toString()}")
        else:
            print("保存")
    
    def on_save_as(self):
        """另存为"""
        print("另存为")
    
    def on_exit(self):
        """退出"""
        print("退出应用")
    
    # 编辑菜单actions
    def on_undo(self):
        """撤销"""
        print("撤销")
    
    def on_redo(self):
        """重做"""
        print("重做")
    
    def on_cut(self):
        """剪切"""
        print("剪切")
    
    def on_copy(self):
        """复制"""
        print("复制")
    
    def on_paste(self):
        """粘贴"""
        print("粘贴")
    
    def on_preferences(self):
        """首选项"""
        print("打开首选项")
    
    # 视图菜单actions
    def on_zoom_in(self):
        """放大"""
        print("放大")
    
    def on_zoom_out(self):
        """缩小"""
        print("缩小")
    
    def on_zoom_reset(self):
        """重置缩放"""
        print("重置缩放")
    
    def on_toggle_fullscreen(self, checked):
        """
        全屏切换
        
        示例：获取发送信号的 QAction
        """
        action = self.sender()  # 获取发送信号的 QAction
        
        if action:
            # 可以访问 QAction 的各种属性
            print(f"全屏: {checked}")
            print(f"  - Action Text: {action.text()}")
            print(f"  - Action ObjectName: {action.objectName()}")
            print(f"  - Action ToolTip: {action.toolTip()}")
            
            # 访问自定义属性（如果 xml_parser 设置了）
            if hasattr(action, '_icon_name'):
                print(f"  - Icon Name: {action._icon_name}")
        else:
            print(f"全屏: {checked}")
    
    def on_show_dock(self, checked):
        """显示停靠面板"""
        print(f"停靠面板: {checked}")
    
    def on_show_properties(self, checked):
        """显示属性面板"""
        print(f"属性面板: {checked}")
    
    def on_show_output(self, checked):
        """显示输出面板"""
        print(f"输出面板: {checked}")
    
    # 设置菜单actions
    def on_theme_dark(self, checked):
        """切换到暗色主题"""
        if checked:
            print("切换到暗色主题")
    
    def on_theme_light(self, checked):
        """切换到亮色主题"""
        if checked:
            print("切换到亮色主题")
    
    # 工具菜单actions
    def on_schematic_editor(self):
        """原理图编辑器"""
        print("打开原理图编辑器")
    
    def on_pcb_editor(self):
        """PCB编辑器"""
        print("打开PCB编辑器")
    
    def on_design_rule_check(self):
        """设计规则检查"""
        print("执行设计规则检查")
    
    # 窗口菜单actions
    def on_new_window(self):
        """新建窗口"""
        print("新建窗口")
    
    def on_close_window(self):
        """关闭窗口"""
        print("关闭窗口")
    
    def on_tile_horizontal(self):
        """水平平铺"""
        print("水平平铺")
    
    def on_tile_vertical(self):
        """垂直平铺"""
        print("垂直平铺")
    
    def on_cascade(self):
        """层叠"""
        print("层叠窗口")
    
    # 帮助菜单actions
    def on_documentation(self):
        """文档"""
        print("打开文档")
    
    def on_about(self):
        """关于"""
        print("关于对话框")
    
    # 工具栏控件回调
    def on_view_selector_changed(self, index):
        """视图选择器改变"""
        print(f"视图切换到索引: {index}")


class DefaultActionHandler(ActionHandler):
    """默认Action处理器实现"""
    
    def register_actions(self):
        """注册所有action回调"""
        # 文件菜单
        self.registry.register('new_project', self.on_new_project)
        self.registry.register('open_project', self.on_open_project)
        self.registry.register('save', self.on_save)
        self.registry.register('save_as', self.on_save_as)
        self.registry.register('exit', self.on_exit)
        
        # 编辑菜单
        self.registry.register('undo', self.on_undo)
        self.registry.register('redo', self.on_redo)
        self.registry.register('cut', self.on_cut)
        self.registry.register('copy', self.on_copy)
        self.registry.register('paste', self.on_paste)
        self.registry.register('preferences', self.on_preferences)
        
        # 视图菜单
        self.registry.register('zoom_in', self.on_zoom_in)
        self.registry.register('zoom_out', self.on_zoom_out)
        self.registry.register('zoom_reset', self.on_zoom_reset)
        self.registry.register('toggle_fullscreen', self.on_toggle_fullscreen)
        self.registry.register('show_dock', self.on_show_dock)
        self.registry.register('show_properties', self.on_show_properties)
        self.registry.register('show_output', self.on_show_output)
        
        # 设置菜单
        self.registry.register('theme_dark', self.on_theme_dark)
        self.registry.register('theme_light', self.on_theme_light)
        self.registry.register('preferences', self.on_preferences)
        
        # 工具菜单
        self.registry.register('schematic_editor', self.on_schematic_editor)
        self.registry.register('pcb_editor', self.on_pcb_editor)
        self.registry.register('design_rule_check', self.on_design_rule_check)
        
        # 窗口菜单
        self.registry.register('new_window', self.on_new_window)
        self.registry.register('close_window', self.on_close_window)
        self.registry.register('tile_horizontal', self.on_tile_horizontal)
        self.registry.register('tile_vertical', self.on_tile_vertical)
        self.registry.register('cascade', self.on_cascade)
        
        # 帮助菜单
        self.registry.register('documentation', self.on_documentation)
        self.registry.register('about', self.on_about)
        
        # 工具栏控件
        self.registry.register('view_selector', self.on_view_selector_changed)
